package com.yummy.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yummy.dto.QuerySetmealVo;
import com.yummy.dto.SetmealDto;
import com.yummy.entity.*;
import com.yummy.mapper.CategoryMapper;
import com.yummy.mapper.SetmealDishMapper;
import com.yummy.mapper.SetmealMapper;
import com.yummy.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    SetmealMapper setmealMapper;

    @Autowired
    CategoryMapper categoryMapper;

    @Autowired
    SetmealDishMapper setmealDishMapper;

    /**
     * 新增套餐
     */
    @Override
    public boolean addSetmeal(SetmealDto setmealDto) {
        //先保存套餐基本信息   insert Setmeal
//        int i = setmealMapper.insertSetmeal(setmealDto);

        //todo 需要返回id
        long l = setmealMapper.insertSetmealDto(setmealDto);

        //给每一个设置好套餐id
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        //todo 将id存入SetmealDish中，然后添加到表中

        //再保存中间表，就是套餐与菜品关联表
//        int insert = 1;
//        for (SetmealDish setmealDish : setmealDishes) {
//            insert = setmealDishMapper.insert(setmealDish);
//        }
//
//        System.out.println(insert);
//
//        return insert == 1 ? true : false;
        int i = setmealDishMapper.insertBatch(setmealDishes);

        System.out.println(i);
        return true;
    }

    @Override
    public PageInfo<SetmealDto> queryByPageAndCondition(QuerySetmealVo querySetmealVo) {
        //查询所有
        Page<SetmealDto> page = PageHelper.startPage(querySetmealVo.getCurrentPage(), querySetmealVo.getPageSize());

        setmealMapper.selectAllByCondition(querySetmealVo);

        PageInfo<SetmealDto> setmealPageInfo = page.toPageInfo();

        List<SetmealDto> setmealList = setmealPageInfo.getList();

        ArrayList<SetmealDto> setmealDtos = new ArrayList<>();

        for (int i = 0; i < setmealList.size(); i++) {

            SetmealDto setmealDto = JSON.parseObject(JSON.toJSONString(setmealList.get(i)), SetmealDto.class);
            //根据  categoryId 填充 categoryName |
            Category category = categoryMapper.selectByPrimaryKey(setmealDto.getCategoryId());
            setmealDto.setCategoryName(category.getName());
            //根据 id 填充 List<DishFlavor> flavors
            List<SetmealDish> setmealDishes = setmealDishMapper.selectSetmealDishByDishId(querySetmealVo.getId());
            setmealDto.setSetmealDishes(setmealDishes);
            setmealDtos.add(setmealDto);
        }
        setmealPageInfo.setList(setmealDtos);

        return setmealPageInfo;
    }

    @Override
    public boolean editSetmeal(Setmeal setmeal) {
        return setmealMapper.updateBySetmeal(setmeal) > 0;
    }

    @Override
    public boolean removeSetmeals(String... ids) {
        return setmealMapper.deleteBySetmealPrimaryKeys(ids) > 0;
    }

    @Override
    public boolean updateStatus(String id, String status) {
        return setmealMapper.updateStatusById(id, status) > 0;
    }

    @Override
    public Setmeal findSetmealById(String id) {
        Setmeal setmeal = setmealMapper.selectBySetmealId(id);
        return setmeal;
    }

    @Override
    public boolean addSetmealDto(SetmealDto setmealDto) {
        return false;
    }

}
